<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 订单模型
 */
class Order extends Model
{

    // 表名
    protected $name = 'order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    public function order_add_together($user_id,$order,$goods_id_array){
        $order_no = $this->orderNo();
        $order['order_no'] = $order_no;
        $order['user_id'] = $user_id;

        foreach ($goods_id_array as $key => $value) {
            $goods_data = Db::name('litestore_goods')->where(['goods_id'=>$value['goods_id']])->find();
            // deduct_stock_type 库存计算方式:10=下单减库存,20=付款减库存
            if($goods_data['deduct_stock_type']==10){
                Db::name('litestore_goods_spec')->where('goods_spec_id',$value['goods_spec_id'])->setDec('stock_num',$value['num']);//减库存
            }
        }

        $this->save($order);
        return $order;
    }

    public function order_add($user_id, $order){

        // Db::startTrans();
        // 记录订单信息
        $goods_data = Db::name('litestore_goods')->where(['goods_id'=>$order['goods_id']])->find();
        if(empty($goods_data))return 3;//产品信息为空

        $spec_data = Db::name('litestore_goods_spec')->where('goods_spec_id',$order['goods_spec_id'])->find();
        if(empty($spec_data))return 4;//产品多规格数据丢失，请重新下单
        // return(intval($order['num']));
        if(intval($order['num'])>intval($spec_data['stock_num'])){
            return 2;die;
        }
        // deduct_stock_type 库存计算方式:10=下单减库存,20=付款减库存
        if($goods_data['deduct_stock_type']==10){
            Db::name('litestore_goods_spec')->where('goods_spec_id',$order['goods_spec_id'])->setDec('stock_num',$order['num']);//减库存
        }

        $order_no = $this->orderNo();

        $order['goods_attr'] = '';
        $order['rent'] = $order['rent_date'];

        // 单规格情况下
        if($goods_data['spec_type']=='10'){

            // 如果选择了租机
            if(!empty($order['rent_date'])){
                // 根据 goods_spec_id 找到 租期价格数据
                $rent_money_array = $spec_data['rent_array'];
                $rent_money_array = json_decode($rent_money_array,true);

                //商品规格租期描述
                $order['goods_attr'] = $order['rent_date']; 

                $order['unit_price'] = $spec_data['day_price'];

            }else{
                //正常单规格买
                $order['unit_price'] = $spec_data['goods_price'];//单价
            }
            $order['cover'] = $goods_data['cover'];
            $order['spec_type'] = '10';    
        }else{
            //多规格的情况下

            // 查询选择了的属性值
            $spec_sku_id_array = explode('_', $spec_data['spec_sku_id']);
            foreach ($spec_sku_id_array as $key => $value) {
                $order['goods_attr'] .= Db::name('litestore_spec_value')->where(['id'=>$value])->value('spec_value').'+';
            }
            // 如果选择了租机
            if(!empty($order['rent_date'])){
                // 根据 goods_spec_id 找到 租期价格数据
           
                $order['goods_attr'] .= $order['rent_date'].'+'; 

                $order['unit_price'] = $spec_data['day_price'];

            }else{
                //正常单规格买
                $order['unit_price'] = $spec_data['goods_price'];//单价
            }
            $order['cover'] = $spec_data['spec_image'];
            $order['spec_type'] = '20';    

        }
        $order['goods_attr'] = rtrim($order['goods_attr'],'+');

        $order['order_no'] = $order_no;
        $order['goods_name'] = $goods_data['goods_name'];
        
        $order['user_id'] = $user_id;
        $order['createtime'] = time();
        $order['updatetime'] = time();
        unset($order['rent_date']);
        $add_id = $this->save($order);

        empty($add_id);
        Db::commit();
        return $order;
    }

    protected function orderNo()
    {
        return 'WM'.date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

}
